﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.Resources;

public partial class admin_OrderList : SitePage
{
    #region ViewState
    public int count = 0;
    busCustomer _busCus = new busCustomer();
    busStoreOrder _busOrder = new busStoreOrder();
    public string paraURL
    {
        get
        {
            object o = ViewState["paraURL"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["paraURL"] = value; }

    }
    public int F_PageIndex
    {

        get
        {

            object o = ViewState["F_PageIndex"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["F_PageIndex"] = value; }

    }
    public int F_PageSize
    {

        get
        {

            object o = ViewState["F_PageSize"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["F_PageSize"] = value; }

    }

    public string F_SortField
    {

        get
        {
            object o = ViewState["F_SortField"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["F_SortField"] = value; }

    }
    public string F_SortExpression
    {

        get
        {
            object o = ViewState["F_SortExpression"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["F_SortExpression"] = value; }

    }

    #endregion

    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {


            LoadCustomer();
            GetParamater();
            LoadData();
        }
        //  Response.Write("Application:" + this.ApplicationName);
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    #endregion

    #region Events
    protected void Pager2_PageIndexChanging(object obj, PageIndexChangeEventArgs e)
    {
        this.F_PageIndex = e.PageIndex;
        LoadData();
    }
    protected void Pager2_PageSizeChanging(object obj, PageSizeChangeEventArgs e)
    {
        this.F_PageIndex = 1;
        this.F_PageSize = Pager2.PageSize = Pager1.PageSize = e.PageSize;
        LoadData();
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            StoreOrder objData = (StoreOrder)e.Item.DataItem;
            if (objData == null)
                return;
            hidID.Value += objData.OrderId.ToString() + ";";

            Literal ltr = (Literal)e.Item.FindControl("ltrCustomer");
            if (ltr != null)
            {
                Customer objCus = _busCus.GetById(objData.CustomerId);
                if (objCus != null)
                    ltr.Text = objCus.Name;
            }
            ltr = (Literal)e.Item.FindControl("ltrFinishDate");
            if (ltr != null)
            {
                if (objData.FinishDate != DateTime.MinValue)
                    ltr.Text = clsConvert.String(objData.FinishDate);
                else ltr.Text = "-";
            }
            ltr = (Literal)e.Item.FindControl("ltrDetail");
            if (ltr != null)
            {
                clsSQL sql = new clsSQL();
                int count = (int)sql.ExecuteScalar("Select Count(*) from StoreOrderDetail where OrderId=" + objData.OrderId);
                ltr.Text = "<a href='OrderDetail.aspx?OrderId=" + objData.OrderId + "&" + paraURL + "'>" + count + "<a/>";
            }
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE + "');");
            imgButton.CommandArgument = objData.OrderId.ToString();
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
            System.Web.UI.HtmlControls.HtmlTableRow row = (System.Web.UI.HtmlControls.HtmlTableRow)e.Item.FindControl("trRow");
            if (e.Item.ItemIndex % 2 == 0)
                row.Attributes.Add("class", "alt-row");

        }

    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        if (e.CommandName == "SortOrder")
        {
            SortList(e.CommandArgument.ToString());

        }
        else
        {
            int id = clsConvert.Int(e.CommandArgument.ToString());
            switch (e.CommandName)
            {


                case "Delete":
                    {
                        if (Delete(id))
                            LoadData();
                        break;
                    }
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderList.aspx");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.F_SortField = busStoreOrder.OrderId;
        this.F_SortExpression = "ASC";
        this.F_PageIndex = 1;
        LoadData();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string[] arr = hidIDSelect.Value.Split(';');
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].ToString() != string.Empty)
            {
                Delete(clsConvert.Int(arr[i].ToString()));
            }
        }
        LoadData();

    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderAdd.aspx?" + paraURL);
    }

    #endregion

    #region Methods
    private void LoadCustomer()
    {

        CustomerCollection lstCust = _busCus.GetAll();
        F_ddlCustomer.Items.Clear();
        F_ddlCustomer.Items.Add(new ListItem("- - -", ""));
        foreach (Customer objCus in lstCust)
        {
            F_ddlCustomer.Items.Add(new ListItem(objCus.Name, objCus.CustomerId.ToString()));
        }

    }
    private void InitParamater()
    {

    }
    private void SortList(string F_SortField)
    {
        if (this.F_SortField == F_SortField)
        {
            if (this.F_SortExpression == "ASC")
                this.F_SortExpression = "DESC";
            else this.F_SortExpression = "ASC";
        }
        else
        {
            this.F_SortField = F_SortField;
            this.F_SortExpression = "ASC";
        }
        LoadData();
    }


    private bool Delete(int id)
    {

        return _busOrder.Delete(id);
    }

    private void LoadData()
    {
        // return;
        paraURL = GetPageParams(FilterFieldType.All);
        hidID.Value = string.Empty;
        //get Condition
        StoreOrder objSearch = new StoreOrder();
        objSearch.CurrentPage = this.F_PageIndex;
        objSearch.OrderBy = this.F_SortField;
        objSearch.OrderDirection = this.F_SortExpression;
        objSearch.PageSize = Pager1.PageSize;
        objSearch.Condition = " 1=1";
        if (F_txtPurchaseOrder.Text != string.Empty)
            objSearch.Condition += " and PurchaseOrder like N'%" + F_txtPurchaseOrder.Text + "%'";
        if (F_txtTrackingOrder.Text != string.Empty)
            objSearch.Condition += " and TrackingOrder like N'%" + F_txtTrackingOrder.Text + "%'";

        if (F_ddlCustomer.SelectedValue != string.Empty)
            objSearch.Condition += " and CustomerId=" + F_ddlCustomer.SelectedValue.ToString();
        DateTime fromEndDate = clsConvert.Date(F_txtFromEndDate.Text.Trim());
        DateTime toEndDate = clsConvert.Date(F_txtToEndDate.Text.Trim());
        if (fromEndDate != DateTime.MinValue)
        {
            objSearch.Condition += " and datediff(dd,'" + fromEndDate.ToString("yyyy-MM-dd") + "',FinishDate)>=0";
        }
        if (toEndDate != DateTime.MinValue)
        {
            objSearch.Condition += " and datediff(dd,FinishDate,'" + toEndDate.ToString("yyyy-MM-dd") + "')>=0";
        }
        int total = 0;
        StoreOrderCollection lstResult = _busOrder.List(objSearch);
        total = objSearch.TotalRecords;
        if (lstResult != null && lstResult.Count > 0)
        {
            count = lstResult.Count;
            rptData.DataSource = lstResult;
            divGrid.Visible = true;
            divEmplty.Visible = false;
        }
        else
        {
            rptData.DataSource = null;
            divGrid.Visible = false;
            divEmplty.Visible = true;
            divEmplty.InnerHtml = Resources.Resource.MSG_EMPTY;
        }

        rptData.DataSource = lstResult;
        rptData.DataBind();
        Pager1.SetPaging(this.F_PageIndex, count, total);
        Pager2.SetPaging(this.F_PageIndex, count, total);
    }
    private void GetParamater()
    {

        F_txtPurchaseOrder.Text = clsQueryString.GetString("F_txtPurchaseOrder");
        F_txtPurchaseOrder.Text = clsQueryString.GetString("F_txtPurchaseOrder");
        F_txtFromEndDate.Text = clsQueryString.GetString("F_txtFromEndDate");
        F_txtToEndDate.Text = clsQueryString.GetString("F_txtToEndDate");
        F_ddlCustomer.SelectedValue = clsQueryString.GetString("F_ddlCustomer");
        this.F_SortField = clsQueryString.GetString("F_SortField");
        if (this.F_SortField == string.Empty)
            this.F_SortField = "TrackingOrder";
        this.F_SortExpression = clsQueryString.GetString("F_SortExpression");
        if (this.F_SortExpression == string.Empty)
            this.F_SortExpression = "ASC";
        F_PageIndex = clsQueryString.GetInt("F_PageIndex");
        F_PageSize = clsQueryString.GetInt("F_PageSize");
        if (F_PageSize < 1)
            F_PageSize = clsConfigData.DEFAULT_ADMIN_PAGE_SIZE;
        if (F_PageIndex < 1)
            F_PageIndex = 1;
        this.Pager2.PageSize = this.Pager1.PageSize = F_PageSize;
        //F_ddlIsAvtive.SelectedValue = clsQueryString.GetString("F_ddlIsAvtive");
    }
    #endregion
}